package can.bishe.modules.exam.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import can.bishe.common.utils.SubjectExeclReader;
import can.bishe.common.validator.ValidatorUtils;
import can.bishe.modules.exam.dao.ExamOptionDao;
import can.bishe.modules.exam.dao.ExamSubjectDao;
import can.bishe.modules.exam.entity.ExamOptionEntity;
import can.bishe.modules.exam.entity.ExamSubjectOptionEntity;
import can.bishe.modules.exam.entity.SubjectOptionDTO;
import can.bishe.modules.exam.service.ExamOptionService;
import can.bishe.modules.exam.service.ExamSubjectOptionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import can.bishe.modules.exam.entity.ExamSubjectEntity;
import can.bishe.modules.exam.service.ExamSubjectService;
import can.bishe.common.utils.PageUtils;
import can.bishe.common.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * 题目信息
 *
 * @author can
 * @email 822024417@qq.com
 * @date 2021-04-28 23:03:48
 */
@RestController
@RequestMapping("exam/examsubject")
public class ExamSubjectController {
    @Autowired
    private ExamSubjectService examSubjectService;
    @Autowired
    private ExamOptionService examOptionService;
    @Autowired
    private ExamSubjectOptionService examSubjectOptionService;
    @Resource
    private ExamSubjectDao examSubjectDao;
    @Resource
    private ExamOptionDao examOptionDao;
    // 控制台打印
    private final Logger log = LoggerFactory.getLogger(getClass());
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = examSubjectService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 选择题目列表
     */
    @RequestMapping("/subjectList")
    public R subjectList(@RequestParam Map<String, Object> params){
        List<ExamSubjectEntity> data = examSubjectService.list();
        return R.ok().put("data",data);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{subjectId}")
    public R info(@PathVariable("subjectId") Long subjectId){
        ExamSubjectEntity examSubject = examSubjectService.getById(subjectId);
        List<ExamSubjectOptionEntity> examSubjectOptionEntities = examSubjectOptionService.list(new QueryWrapper<ExamSubjectOptionEntity>().
                eq("subject_id",subjectId));
        List<ExamOptionEntity> optionEntities = new ArrayList<>();
        for (ExamSubjectOptionEntity examSubjectOptionEntityOne:examSubjectOptionEntities) {
            optionEntities.add(examOptionService.getById(examSubjectOptionEntityOne.getOptionId()));
        }
        return R.ok()
                .put("examSubject", examSubject)
                .put("examOptionList", optionEntities);
    }
    /**
     * 保存
     */
    @RequestMapping("/upload")
    public R upload(@RequestBody MultipartFile file){
//        examSubjectService.save(examSubject);
        String filePath = "D://";
        String fileName = file.getOriginalFilename();
        File targetFile = new File(filePath+fileName);
        try {
            file.transferTo(targetFile);
            System.out.println("上传成功，文件名：" + targetFile);
        } catch (IOException ioe) {
            System.out.println("读写异常");
        }
        SubjectExeclReader rd = new SubjectExeclReader();
        try{
            rd.readExcel(filePath + fileName);
        }catch (IOException ioe){
            System.out.println("读写异常");
        }
        return R.ok();
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SubjectOptionDTO subjectOptionDTO){
//        examSubjectService.save(examSubject);
        log.info("入参{}",subjectOptionDTO.toString());
        ExamSubjectEntity examSubjectEntity = subjectOptionDTO.getExamSubjectEntity();
        if(examSubjectEntity.getSubjectType().equals("1")){
            List<ExamOptionEntity> examOptionEntityList = subjectOptionDTO.getExamOptionEntityList();
            examSubjectDao.insert(examSubjectEntity);
            Long subjectId = examSubjectEntity.getSubjectId();
            for (ExamOptionEntity examOptionEntityOne:examOptionEntityList) {
                examOptionDao.insert(examOptionEntityOne);
                Long optionId = examOptionEntityOne.getOptionId();
                ExamSubjectOptionEntity subjectOptionEntity = new ExamSubjectOptionEntity();
                subjectOptionEntity.setSubjectId(subjectId);
                subjectOptionEntity.setOptionId(optionId);
                examSubjectOptionService.save(subjectOptionEntity);
            }
        }else {
            examSubjectDao.insert(examSubjectEntity);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SubjectOptionDTO subjectOptionDTO){
        log.info("入参{}",subjectOptionDTO.toString());
        ExamSubjectEntity examSubjectEntity = subjectOptionDTO.getExamSubjectEntity();
        List<ExamOptionEntity> examOptionEntityList = subjectOptionDTO.getExamOptionEntityList();
        examSubjectDao.updateById(examSubjectEntity);
        Long subjectId = examSubjectEntity.getSubjectId();
        for (ExamOptionEntity examOptionEntityOne:examOptionEntityList) {
            examOptionDao.updateById(examOptionEntityOne);
            Long optionId = examOptionEntityOne.getOptionId();
            ExamSubjectOptionEntity subjectOptionEntity = new ExamSubjectOptionEntity();
            subjectOptionEntity.setSubjectId(subjectId);
            subjectOptionEntity.setOptionId(optionId);
            examSubjectOptionService.updateById(subjectOptionEntity);
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] subjectIds){
        examSubjectService.removeByIds(Arrays.asList(subjectIds));

        return R.ok();
    }

}
